<template>
  <el-dialog title="添加角色" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :rules="rules" :model="formData" label-width="100px">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <template>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addRole, getAssignRole, editRole } from '@/api/model/role.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        id: null,
        title: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '角色名称不为空', tirgger: 'blur' },
          { min: 1, max: 4, message: '名称最少1~4个字符', tirgger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取角色详情
    async getAssignRole(id) {
      this.formData.id = id
      const res = await getAssignRole(id)
      console.log('2222', res)
      // 回显
      this.formData = res.data
    },
    // 确定
    submit() {
      // 表单校验
      this.$refs.form.validate(async bool => {
        if (bool) {
          if (this.formData.id) {
            // 编辑
            await editRole(this.formData)
          } else {
            // 如校验通过  则调用新增接口
            await addRole(this.formData)
          }
          //   关闭弹窗
          this.closeFn()
          // 更新浏览器中显示的列表
          this.$emit('updateEvent')
        }
      })
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      this.formData = {
        title: '',
        description: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style scoped>

</style>
